TextDataGridEditorController = Class.create(DataGridEditorController, {
  buildEditor: function ($super, name, columnDefinition) {
    var editor = this._createEditorElement("input", name, "dataGridCellEditorText", {name: name, type: "text"}, columnDefinition);

    if (columnDefinition.required)
      editor.addClassName("required");

    this._editorValueChangeListener = this._onEditorValueChange.bindAsEventListener(this);
    Event.observe(editor, "change", this._editorValueChangeListener);
    return editor;
  },
  buildViewer: function ($super, name, columnDefinition) {
    return this._createViewerElement("div", name, "dataGridCellViewerText", {}, columnDefinition);
  },
  attachContentHandler: function ($super, dataGrid, cell, handlerInstance) {
    var handlerInstance = $super(dataGrid, cell, handlerInstance);
    handlerInstance._clickListener = this._onClick.bindAsEventListener(this);
    Event.observe(handlerInstance, "click", handlerInstance._clickListener); 
  },
  detachContentHandler: function ($super, handlerInstance) {
    Event.stopObserving(handlerInstance, "click", handlerInstance._clickListener);
    handlerInstance._clickListener = undefined;
    $super(handlerInstance);
  }, 
  disableEditor: function ($super, handlerInstance) {
    if (handlerInstance._editable == false)
      handlerInstance.addClassName("dataGridCellViewerDisabled");
    else {
      this._addDisabledHiddenElement(handlerInstance);
    }
    handlerInstance.disabled = true;
  },
  enableEditor: function ($super, handlerInstance) {
    if (handlerInstance._editable != true)
      handlerInstance.removeClassName("dataGridCellViewerDisabled");
    else {
      this._removeDisabledHiddenElement(handlerInstance);
    }
    handlerInstance.disabled = false;
  },
  getEditorValue: function ($super, handlerInstance) {
    if (handlerInstance._editable != true) 
      return this._getViewerValue(handlerInstance);
    else
      return handlerInstance.value;
  },
  setEditorValue: function ($super, handlerInstance, value) {
    value = this._unescapeHtmlEntities(value);
    if (handlerInstance._editable != true) {
      this._setViewerValue(handlerInstance, value);
    } else {
      if (this.isDisabled(handlerInstance))
        this._updateDisabledHiddenElement(handlerInstance, value);
      handlerInstance.value = value;
    }
  },
  destroyEditor: function ($super, handlerInstance) {
    handlerInstance.remove();
  },
  isDisabled: function ($super, handlerInstance) {
    return handlerInstance.disabled == true;
  },
  getDataType: function ($super) {
    return "text";  
  },
  getMode: function ($super) { 
    return DataGridControllers.EDITMODE_EDITABLE;
  },
  _onClick: function (event) {
    var handlerInstance = Event.element(event);
    
    if (!handlerInstance.hasClassName("dataGridCellEditorText") && !handlerInstance.hasClassName("dataGridCellViewerText")) {
      var e = handlerInstance.up(".dataGridCellViewerText");
      if (e)
        handlerInstance = e;
      else {
        var e = handlerInstance.up(".dataGridCellEditorText");
        if (e)
          handlerInstance = e;
      }
    }
    
    if (Object.isFunction(handlerInstance._columnDefinition.onclick)) {
      if (this.isDisabled(handlerInstance) != true) { 
        handlerInstance._columnDefinition.onclick.call(window, {
          dataGridComponent: handlerInstance._dataGrid,
          row: this.getEditorRow(handlerInstance),
          column: this.getEditorColumn(handlerInstance)
        });
      }
    }
  },
  _onEditorValueChange: function (event) {
    var handlerInstance = Event.element(event);
    this._fireValueChange(handlerInstance, handlerInstance.value);
  }
});

DataGridControllers.registerController(new TextDataGridEditorController());